/*
 * Classname: MoneyConvert.java
 * 
 * Version information
 *
 * Date: 2007-9-11
 * 
 * Copyright DMX
 */

package com.dmx.fw.util;

import java.text.DecimalFormat;

/**
 * Description: 将数字型转换成中文的大写货币值
 * 
 * @author Peter Wei
 * @version 1.0 2007-9-11
 */
public class MoneyConvert {

	public static final char[] UNIT = { '亿', '十', '百', '千', '万', '十', '百', '千' };

	public static final char[] CAPITAL_LETTER = { '零', '壹', '贰', '叁', '肆', '伍',
			'陆', '柒', '捌', '玖' };

	public static final char[] MONEY_UNIT = { '元', '角', '分', '里' };

	/**
	 * 整数部分的转换
	 * 
	 * @param moneyStr
	 *            整数部分的数字
	 * @return 大写货币值
	 * @throws NumberFormatException
	 */
	protected static String convertInteger(String moneyStr)
			throws NumberFormatException {

		StringBuffer capitalMoney = new StringBuffer();

		String reverseString = new StringBuffer(moneyStr).reverse().toString();

		int len = moneyStr.length();

		// 由字符转换的数字
		int moneyNumber = 0;

		// 是否补0
		boolean addZero = false;

		for (int i = 0; i < len; i++) {

			if (!Character.isDigit(reverseString.charAt(i))) {
				throw new NumberFormatException("不是正确的数字格式");
			}

			moneyNumber = Character.digit(reverseString.charAt(i), 10);

			// 如果当前位是0,开启补0继续循环直到不是0的位
			if (moneyNumber == 0) {
				addZero = true;
			} else {
				// 若当前位不是第一位且补0开启
				if (addZero && capitalMoney.length() != 0) {
					capitalMoney.append(CAPITAL_LETTER[0]);
				}
				// 插入单位
				if (i > 0) {
					capitalMoney.append(UNIT[i % UNIT.length]);
				} else {
					capitalMoney.append(MONEY_UNIT[0]);
				}
				// 插入大写数字
				capitalMoney.append(CAPITAL_LETTER[moneyNumber]);
				addZero = false;
			}

		}

		if (capitalMoney.length() == 0) {
			return Character.toString(CAPITAL_LETTER[0]);
		}

		return capitalMoney.reverse().toString();

	}

	/**
	 * 小数部分的转换
	 * 
	 * @param decimalStr
	 *            小数部分的数字
	 * @return 大写货币值
	 * @throws NumberFormatException
	 */
	protected static String convertDecimal(String decimalStr)
			throws NumberFormatException {

		StringBuffer capitalMoney = new StringBuffer();

		// 由字符转换的数字
		int moneyNumber = 0;

		// 是否补0
		boolean addZero = false;

		for (int i = 0; i < decimalStr.length() && i < MONEY_UNIT.length; i++) {
			if (!Character.isDigit(decimalStr.charAt(i))) {
				throw new NumberFormatException("不是正确的数字格式");
			}

			moneyNumber = Character.digit(decimalStr.charAt(i), 10);

			// 如果当前位是0,开启补0继续循环直到不是0的位
			if (moneyNumber == 0) {
				addZero = true;
			} else {
				if (addZero) {
					capitalMoney.append(CAPITAL_LETTER[0]);
				}
				// 插入大写数字
				capitalMoney.append(CAPITAL_LETTER[moneyNumber]);
				// 插入单位
				capitalMoney.append(MONEY_UNIT[i + 1]);
				addZero = false;
			}

		}

		return capitalMoney.toString();

	}

	/**
	 * 将数字型转换成中文的大写货币值,带小数点(精确到3位)
	 * 
	 * @param moneyStr
	 *            数字型字符串
	 * @return 大写货币值
	 * @throws NumberFormatException
	 */
	public static String convertToCapital(String moneyStr)
			throws NumberFormatException {

		String[] number = moneyStr.split("\\.", 2);

		if (number.length > 1) {
			return convertInteger(number[0]) + convertDecimal(number[1]);
		}

		return convertInteger(number[0]);

	}

	/**
	 * 将数字转换成中文的大写货币值,带小数点(精确到3位)
	 * 
	 * @param doubleNumber
	 *            double型数字
	 * @return 大写货币值
	 * @throws NumberFormatException
	 */
	public static String convertToCapital(double doubleNumber)
			throws NumberFormatException {

		DecimalFormat decimalFormat = new DecimalFormat("######.000");
		String moneyStr = decimalFormat.format(Math.abs(doubleNumber));
		return convertToCapital(moneyStr);

	}
    
	/**
	 *   得到一个数的中文数组 如: 1210.00  {"壹","贰","壹","零","零","零"}
	 * @param number
	 * @return
	 */
	public static String[] getCapitalNomber(String number){
		String s=String.valueOf(Math.abs(Double.valueOf(number)));
		int iPos=s.indexOf(".");
		String[] nub=new String[10];
		StringBuffer buf=new StringBuffer();
		StringBuffer buf2=new StringBuffer();
		if(iPos>0) {
			buf.append(s.substring(0, iPos));
			if(s.length()-iPos>2){
				buf.append(s.substring(iPos+1, iPos+3));
			}else{
				s=s+"00";
				buf.append(s.substring(iPos+1, iPos+3));
			}
		}else{
			buf.append(s);
			buf.append("00");
		}
		String str=buf.toString();
		//System.out.println(str);
		for (int j = 0; j <str.length(); j++) {
			buf2.append(MoneyConvert.convertToChinese(str.substring(j, j+1)));
		}
		String str2=buf2.toString();
		for (int j = 0,k=str2.length()-1; j <str2.length(); j++,k--) {
			nub[k]=str2.substring(j,j+1);
		}		
		return nub;
	}
	/**
	 *   得到一个数的数组 如: 1210.00  {"1","2","1","0","0","0"}
	 * @param number
	 * @return
	 */
   public static String[] getNomberArray(String number){
		String s=number;
		int i=s.indexOf(".");
		String[] nub=new String[10];
		StringBuffer buf=new StringBuffer();
		//StringBuffer buf2=new StringBuffer();
		if(i>0) {
			buf.append(s.substring(0, i));
			if(s.length()-i>2){
				buf.append(s.substring(i+1, i+3));
			}else{
				s=s+"00";
				buf.append(s.substring(i+1, i+3));
			}
		}else{
			buf.append(s);
			buf.append("00");
		}	
		String str2=buf.toString();
		//System.out.println(str2);
		for (int j = 0,k=str2.length()-1; j <str2.length(); j++,k--) {
			nub[k]=str2.substring(j,j+1);
		}		
		return nub;
	}
	
   
     //将数字转换成中文
	 public static  String  convertToChinese(String str){
			 int d=Math.abs(Integer.valueOf(str).intValue());	
			 switch(d){
	         case 0:
	        	 return str.replace('0', '零');
	         case 1:
	         return str.replace('1', '壹');
	         case 2:
	        	 return str.replace('2', '贰');
	         case 3:
	        	 return str.replace('3', '叁');
	         case 4:
	        	 return str.replace('4', '肆');
	         case 5:
	        	 return str.replace('5', '伍');
	         case 6:
	        	 return str.replace('6', '陆');
	         case 7:
	        	 return str.replace('7', '柒');
	         case 8:
	        	 return str.replace('8', '捌');
	         case 9:
	        	 return str.replace('9', '玖');	
	         default : return "零";

	        }
		}
	/**
	 * test MoneyConvert
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String s = convertInteger("58831055");
		String s2 = convertDecimal("010");
		String all = convertToCapital("23398918330008.008");
		double doubleNumber = 16880008.802d;
		String all2 = convertToCapital(doubleNumber);
		System.out.println(s);
		System.out.println(s2);
		System.out.println(all);
		System.out.println(all2);

	}

}
